package com.example.myapplication;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MsgAdapter extends ArrayAdapter<Msg> {
    private int resourceId;

    public MsgAdapter(Context context, int textViewresourceId, List<Msg> objects) {
        super(context, textViewresourceId, objects);
        resourceId = textViewresourceId;
    }

  /*  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Msg msg = getItem(position);
        View view;
        ViewHolder viewHolder;

        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);
            viewHolder.le=(ImageView)view.findViewById(R.id.lemsg_image);
            viewHolder.ri=(ImageView)view.findViewById(R.id.rimsg_image);
            viewHolder.rightLayout = (LinearLayout)view.findViewById(R.id.right_Layout);
            viewHolder.leftMsg = (TextView)view.findViewById(R.id.left_msg);
            viewHolder.rightMsg = (TextView)view.findViewById(R.id.right_msg);
            viewHolder.le.setImageResource(msg.getImageId());
            viewHolder.ri.setImageResource(msg.getImageId());
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        if(msg.getType()==Msg.RECEIVED){
            //如果是收到的消息，则显示左边消息布局，将右边消息布局隐藏
           viewHolder.leftLayout.setVisibility(View.VISIBLE);
           viewHolder.rightLayout.setVisibility(View.GONE);
            viewHolder.leftMsg.setText(msg.getContent());
        }else if(msg.getType()==Msg.SENT){
            //如果是发出去的消息，显示右边布局的消息布局，将左边的消息布局隐藏
            viewHolder.rightLayout.setVisibility(View.VISIBLE);
            viewHolder.leftLayout.setVisibility(View.GONE);
            viewHolder.rightMsg.setText(msg.getContent());
        }
        return view;
    }

    class ViewHolder{
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rightMsg;
        ImageView le;
        ImageView ri;
    }*/
  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
      Msg msg = getItem(position);
      View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
      ImageView li =(ImageView) view.findViewById(R.id.lemsg_image);
      TextView lm = (TextView) view.findViewById(R.id.left_msg);
      ImageView ri =(ImageView) view.findViewById(R.id.rimsg_image);
      TextView rm = (TextView) view.findViewById(R.id.right_msg);
      LinearLayout leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);
      LinearLayout rightLayout = (LinearLayout)view.findViewById(R.id.right_Layout);
      li.setImageResource(msg.getImageId());
      ri.setImageResource(msg.getImageId());
      rm.setText(msg.getContent());
      if(msg.getType()==Msg.RECEIVED){
          //如果是收到的消息，则显示左边消息布局，将右边消息布局隐藏
          leftLayout.setVisibility(View.VISIBLE);
          rightLayout.setVisibility(View.GONE);
          lm.setText(msg.getContent());
      }else if(msg.getType()==Msg.SENT){
          //如果是发出去的消息，显示右边布局的消息布局，将左边的消息布局隐藏
          rightLayout.setVisibility(View.VISIBLE);
          leftLayout.setVisibility(View.GONE);
          rm.setText(msg.getContent());
      }
      return view;
  }
}
